package com.yjl.hotel.customview;

import com.yjl.hotel.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CircleImageView extends ImageView {

	private Bitmap mSrc;
	private int mWidth;
	private int mHeight;
	
	public CircleImageView(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}
	
	public CircleImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);
		int n = a.getIndexCount();
		for(int i = 0; i < n; i++) {
			int attr = a.getIndex(i);
			switch (attr) {
			case R.styleable.CircleImageView_btmSrc:
				mSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));
				break;
			}
		}
		a.recycle();
		
	}
	
	

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		int specMode = MeasureSpec.getMode(widthMeasureSpec);
		int specSize = MeasureSpec.getSize(widthMeasureSpec);
		
		if(specMode == MeasureSpec.EXACTLY) {
			mWidth = specSize;
		} else {
			int desireByImg = getPaddingLeft() + getPaddingRight() + mSrc.getWidth();
			if(specMode == MeasureSpec.AT_MOST){
				mWidth = Math.min(desireByImg, specSize);
			} else {
				mWidth = desireByImg;
			}
		}
		
		specMode = MeasureSpec.getMode(heightMeasureSpec);  
        specSize = MeasureSpec.getSize(heightMeasureSpec);  
        if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate  
        {  
            mHeight = specSize;  
        } else  
        {  
            int desire = getPaddingTop() + getPaddingBottom()  
                    + mSrc.getHeight();  
  
            if (specMode == MeasureSpec.AT_MOST)// wrap_content  
            {  
                mHeight = Math.min(desire, specSize);  
            } else  
                mHeight = desire;  
        }  
        
        setMeasuredDimension(mWidth, mHeight);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		int min = Math.min(mWidth, mHeight);
		mSrc = Bitmap.createScaledBitmap(mSrc, min, min,false);
		canvas.drawBitmap(createCircleBitmap(mSrc, min), 0, 0, null);
	}
	
	private Bitmap createCircleBitmap(Bitmap src, int min) {
		final Paint paint = new Paint();
		paint.setAntiAlias(true);
		Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);
		Canvas canvas = new Canvas(target);
		canvas.drawCircle(min / 2, min / 2, min / 2, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		canvas.drawBitmap(src, 0, 0, paint);
		return target;
	}

}
